/// @tags: DP
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

namespace BlueQuantum {

int const N = 2e2 + 5;
int const dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};

int n, m, ans;
int f[N][N];
char map[N][N];

struct Node {
  int f, pos;
} q[N];

inline void solve(int x, int y, int len, int d) {
  int head = 1, tail = 0;
  for (int i = 1; x >= 1 && x <= n && y >= 1 && y <= m; ++i, x += dx[d], y += dy[d])
    if (map[x][y] == 'x') head = 1, tail = 0;
    else {
      while (head <= tail && q[tail].f + i - q[tail].pos < f[x][y]) tail--;
      q[++tail] = (Node){f[x][y], i};
      if (q[tail].pos - q[head].pos > len) head++;
      f[x][y] = q[head].f + i - q[head].pos;
      ans = max(ans, f[x][y]);
    }
}

inline int main() {
  int x, y, k;
  cin >> n >> m >> x >> y >> k;
  for (int i = 1; i <= n; ++i) cin >> (map[i] + 1);
  memset(f, -0x3f, sizeof(f));
  f[x][y] = 0;
  for (int i = 1, s, t, d, len; i <= k; ++i) {
    cin >> s >> t >> d;
    len = t - s + 1;
    switch (d--) {
      case 1:
        for (int j = 1; j <= m; ++j) solve(n, j, len, d);
        break;
      case 2:
        for (int j = 1; j <= m; ++j) solve(1, j, len, d);
        break;
      case 3:
        for (int j = 1; j <= n; ++j) solve(j, m, len, d);
        break;
      case 4:
        for (int j = 1; j <= n; ++j) solve(j, 1, len, d);
        break;
    }
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P2254 [NOI2005] 瑰丽华尔兹.in", "r", stdin);
  freopen("P2254 [NOI2005] 瑰丽华尔兹.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
